home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / progtool / c / gcc / gempp19.zoo / gem++19 / src / gemfs.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-20  |  3.4 KB  |  179 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  This file is Copyright 1992,1993 by Warwick W. Allison.
  4. //  This file is part of the gem++ library.
  5. //  You are free to copy and modify these sources, provided you acknowledge
  6. //  the origin by retaining this notice, and adhere to the conditions
  7. //  described in the file COPYING.LIB.
  8. //
  9. /////////////////////////////////////////////////////////////////////////////
  10.  
  11. #include "gemfs.h"
  12. #include <osbind.h>
  13. #include <aesbind.h>
  14. #include <string.h>
  15.  
  16.  
  17. static const PATHLEN=128;
  18. static const FILELEN=32;
  19.  
  20. GEMfileselector::GEMfileselector(int maxlen=PATHLEN) :
  21.     filename(new char[maxlen]),
  22.     filespec(new char[maxlen]),
  23.     file(new char[FILELEN]),
  24.     len(maxlen)
  25. {
  26.     CWD();
  27. }
  28.  
  29.  
  30. GEMfileselector::GEMfileselector(char* fname) :
  31.     filename(fname),
  32.     filespec(new char[PATHLEN]),
  33.     file(new char[FILELEN]),
  34.     len(0)
  35. {
  36.     if (fname[0]) Filename(fname);
  37.     else CWD();
  38. }
  39.  
  40.  
  41. GEMfileselector::~GEMfileselector()
  42. {
  43.     if (len) delete filename;
  44.     delete filespec;
  45.     delete file;
  46. }
  47.  
  48.  
  49. const char* GEMfileselector::CWD()
  50. {
  51.     int drive=Dgetdrv();
  52.     filespec[0]=drive+'A';
  53.     filespec[1]=':';
  54.     Dgetpath(&filespec[2],drive+1); // +1 since drv0 = current drive!
  55.     strcat(filespec,"\\*.*");
  56.     strcpy(file,"");
  57.     Merge();
  58.  
  59.     return filename;
  60. }
  61.  
  62.  
  63. // Returns result both ways.  NULL if cancelled.
  64. const char* GEMfileselector::Get(const char* prompt=0, char* into=0)
  65. {
  66.     int okay;
  67.  
  68.     if (prompt) {
  69.         if (fsel_exinput(filespec,file,&okay,(char*)prompt) < 0)
  70.             fsel_input(filespec,file,&okay);
  71.     } else
  72.         fsel_input(filespec,file,&okay);
  73.  
  74.     Merge();
  75.  
  76.     if (okay) {
  77.         if (into) strcpy(into,filename);
  78.         return filename;
  79.     } else {
  80.         return 0;
  81.     }
  82. }
  83.  
  84. void GEMfileselector::Merge()
  85. {
  86.     strcpy(filename,filespec);
  87.     for (int i=0; filename[i]; i++);
  88.     while (i && filename[i]!='\\') i--;
  89.     if (filename[i]=='\\') strcpy(&filename[i+1],file);
  90.     else strcpy(filename,file);
  91. }
  92.  
  93. void GEMfileselector::Path(const char* path)
  94. {
  95.     // Find '\'
  96.     for (int i=0; path[i]; i++);
  97.     int to=i-1;
  98.     for (; i && path[i]!='\\'; i--);
  99.  
  100.     if (path[i]=='\\') {
  101.         while (to && path[to]=='\\') to--; // ignore trailing '\'
  102.  
  103.         for (int j=0; filespec[j]; j++);
  104.         while (j && filespec[j]!='\\') j--;
  105.         if (j) {
  106.             strcpy(&filespec[to+2],&filespec[j+1]);
  107.             strncpy(filespec,path,to+1);
  108.             filespec[to+1]='\\';
  109.         } else {
  110.             strncpy(filespec,path,to+1);
  111.             strcpy(filespec+to+1,"\\*.*");
  112.         }
  113.     } else {
  114.         strncpy(filespec,path,to+1);
  115.         strcpy(filespec+to+1,"\\*.*");
  116.     }
  117.  
  118.     Merge();
  119. }
  120.  
  121. void GEMfileselector::File(const char* f)
  122. {
  123.     strcpy(file,f);
  124.     Merge();
  125. }
  126.  
  127. const char* GEMfileselector::File() const
  128. {
  129.     return file;
  130. }
  131.  
  132. void GEMfileselector::Filespec(const char* fspec)
  133. {
  134.     for (int i=0; fspec[i]; i++);
  135.     while (i && fspec[i]!='\\') i--;
  136.  
  137.     if (fspec[i]=='\\') {
  138.         strcpy(filespec,fspec);
  139.     } else {
  140.         for (int j=0; filespec[j]; j++);
  141.         while (j && filespec[j]!='\\') j--;
  142.         if (filespec[j]=='\\') {
  143.             strcpy(&filespec[j+1],fspec);
  144.         } else {
  145.             strcpy(filespec,fspec);
  146.         }
  147.     }
  148. }
  149.  
  150. const char* GEMfileselector::Filespec() const
  151. {
  152.     return filespec;
  153. }
  154.  
  155. void GEMfileselector::Filename(const char* fname)
  156. // eg. Filename("foo.bar"); Filename("E:\foo\foo.bar"); oldgot=Filename();
  157. {
  158.     for (int i=0; fname[i]; i++);
  159.     while (i && fname[i]!='\\') i--;
  160.  
  161.     if (fname[i]!='\\') {
  162.         File(fname);
  163.     } else {
  164.         File(&fname[i+1]);
  165.         strcpy(filename,fname);
  166.         filename[i]=0;
  167.         Path(filename);
  168.         Merge();
  169.     }
  170. }
  171.  
  172. const char* GEMfileselector::Filename() const
  173. {
  174.     return filename;
  175. }
  176.  
  177.  
  178.  
  179.